package edu.zut.cs.network.exercise.bank.activemq;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import edu.zut.cs.network.exercise.bank.entity.Bank;
import edu.zut.cs.network.exercise.bank.service.BankManager;
import edu.zut.cs.network.exercise.bank.service.impl.BankManagerImpl;



public class BankProducer {

	public List<Bank> bankList=new ArrayList<Bank>();
	public BankManager producerManager=new BankManagerImpl();
	private String userName = "";
	private String password = "";
	private String brokerURL = "tcp://192.168.43.253:61616";
	private ConnectionFactory factory;
	private Connection connection;
	private Session session;
	private Destination destination;
	private MessageProducer producer;

	public void start() {
		try {
			factory = new ActiveMQConnectionFactory(userName, password, brokerURL);
			connection = factory.createConnection();
		    connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			destination = session.createQueue("banks");
			producer = session.createProducer(destination);
			producer.setDeliveryMode(DeliveryMode.PERSISTENT);
			bankList=producerManager.queryAllComp();
			String message=producerManager.json(bankList);
			TextMessage bankMessage=session.createTextMessage(message);
			producer.send(bankMessage);
			producer.close();
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
